package apptoko.com.adapter;

import main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu.OnItemSelectedListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import apptoko.com.ItemBrokerActivity;
import apptoko.com.adapter.AggregateListAdapter.RowType;
import apptoko.com.network.BackupApkTask;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import com.androidquery.AQuery;
import apptoko.com.R;
;

public class ListItem implements Item {

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNewVersion() {
		return newVersion;
	}

	public void setNewVersion(String newVersion) {
		this.newVersion = newVersion;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getSlug() {
		return slug;
	}

	private String name;
    private String currentVersion;
    private String newVersion;
    private Drawable icon;
    private String slug;
    private String avatar;
    private AQuery aq;
    private String packageName;
    private Typeface robotoBoldCondensed;
    private Activity context;
    private IPassItem iPassItem;
    private UpdateClicked updateClickListener;

    public ListItem(Activity context, String name, String slug, String currentVersion, String newVersion, Drawable icon) {
        this.name = name;
        this.context = context;
        this.currentVersion = currentVersion;
        this.newVersion = newVersion;
        this.icon = icon;
        this.slug = slug;
        robotoBoldCondensed = Typeface.createFromAsset(context.getAssets(), context.getResources().getString(R.string.roboto_bold_condensed));
    }
    
    public void setPassItemListener(IPassItem listener){
    	this.iPassItem = listener;
    }
    
    public void setUpdateClickListener(UpdateClicked l){
    	this.updateClickListener = l;
    }
    
    @Override
    public int getViewType() {
        return RowType.LIST_ITEM.ordinal();
    }

    @SuppressLint("NewApi")
	@Override
    public View getView(LayoutInflater inflater, View convertView) {
        ItemViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.app_manage_item, null);
            holder = new ItemViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.manage_app_icon);
            holder.name = (TextView) convertView.findViewById(R.id.manage_name);
            holder.currentVer = (TextView) convertView.findViewById(R.id.manage_current_version);
            holder.newVer = (TextView) convertView.findViewById(R.id.manage_new_version);
            holder.btnMenu = (ImageButton) convertView.findViewById(R.id.btn_quick_menu);
            convertView.setTag(holder);
        } else {
            holder = (ItemViewHolder) convertView.getTag();
        }
        aq = new AQuery(convertView);
        if(icon != null){
        	aq.id(holder.icon).image(icon);
        } else {
        	aq.id(holder.icon).image(avatar, false, true, 0, 0, null, AQuery.FADE_IN);
        }
        aq.id(holder.name).text(name).typeface(robotoBoldCondensed);
        aq.id(holder.currentVer).text(Html.fromHtml(String.format(context.getResources().getString(R.string.current_version), currentVersion))); 
        if(TextUtils.isEmpty(newVersion)){
        	aq.id(holder.newVer).gone();
        } else {
        	aq.id(holder.newVer).text(Html.fromHtml(String.format(context.getResources().getString(R.string.new_version), newVersion))).visible();
        }
        aq.id(holder.btnMenu).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(iPassItem != null){
					iPassItem.onPassItem(ListItem.this);
				}
				if(!TextUtils.isEmpty(newVersion)){
					if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
						PopupMenu menu = new PopupMenu(context, v);
						menu.getMenuInflater().inflate(R.menu.updatable_app_menu, menu.getMenu());
						menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
							@Override
							public boolean onMenuItemClick(MenuItem item) {
								// TODO Auto-generated method stub
								switch (item.getItemId()) {
								case R.id.menu_update:
									if(updateClickListener != null){
										updateClickListener.onUpdateClicked(ListItem.this);
									}
									break;
								case R.id.menu_view_details:
									Intent intent = new Intent(context, ItemBrokerActivity.class);
									intent.putExtra(Constants.PREVIOUS_SCREEN, "manage");
									intent.putExtra(Constants.SLUG, slug);
									intent.putExtra(Constants.PACKAGE_NAME, packageName);
									intent.putExtra(Constants.KEY_REFERER, "direct");
									intent.putExtra(Constants.STORE_KEY, "apps");
									context.startActivity(intent);
									break;
								case R.id.menu_uninstall:
									Utils.uninstallAPK(context, packageName);
									break;
								}
								return true;
							}
						});
						menu.show();
					} else {
						main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
						popupMenuCompat.add(1, R.string.update);
						popupMenuCompat.add(2, R.string.view_details);
						popupMenuCompat.add(3, R.string.uninstall);
						popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
							@Override
							public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
								switch (menuItem.getItemId()) {
								case 1:
									if(updateClickListener != null){
										updateClickListener.onUpdateClicked(ListItem.this);
									}
									break;
								case 2:
									Intent intent = new Intent(context, ItemBrokerActivity.class);
									intent.putExtra(Constants.PREVIOUS_SCREEN, "manage");
									intent.putExtra(Constants.SLUG, slug);
									intent.putExtra(Constants.PACKAGE_NAME, packageName);
									intent.putExtra(Constants.KEY_REFERER, "direct");
									intent.putExtra(Constants.STORE_KEY, "apps");
									context.startActivity(intent);
									break;
								case 3:
									Utils.uninstallAPK(context, packageName);
									break;
								}
							}
						});
						popupMenuCompat.show(v);
					}
				} else {
					if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
						PopupMenu menu = new PopupMenu(context, v);
						menu.getMenuInflater().inflate(R.menu.uptodate_app_menu, menu.getMenu());
						menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
							@Override
							public boolean onMenuItemClick(MenuItem item) {
								switch (item.getItemId()) {
								case R.id.menu_launch:
									Intent launch = context.getPackageManager().getLaunchIntentForPackage(packageName);
									launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									context.startActivity(launch);
									break;
								case R.id.menu_uninstall:
									Utils.uninstallAPK(context, packageName);
									break;
								case R.id.menu_view_details:
									Intent intent = new Intent(context, ItemBrokerActivity.class);
									intent.putExtra(Constants.PREVIOUS_SCREEN, "manage");
									intent.putExtra(Constants.PACKAGE_NAME, packageName);
									intent.putExtra(Constants.KEY_REFERER, "direct");
									intent.putExtra(Constants.STORE_KEY, "apps");
									context.startActivity(intent);
									break;
								case R.id.menu_backup:
									BackupApkTask task = new BackupApkTask(context);
									task.execute(packageName);
									break;
								case R.id.menu_manage_details:
									Utils.installedAppDetails(context, packageName);
									break;
								}
								return true;
							}
						});
						menu.show();
					} else {
						main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
						popupMenuCompat.add(1, R.string.launch);
						popupMenuCompat.add(2, R.string.uninstall);
						popupMenuCompat.add(3, R.string.view_details);
						popupMenuCompat.add(4, R.string.backup);
						popupMenuCompat.add(5, R.string.manage_details);
						popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
							@Override
							public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
								switch (menuItem.getItemId()) {
								case 1:
									Intent launch = context.getPackageManager().getLaunchIntentForPackage(packageName);
									launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									context.startActivity(launch);
									break;
								case 2:
									Utils.uninstallAPK(context, packageName);
									break;
								case 3:
									Intent intent = new Intent(context, ItemBrokerActivity.class);
									intent.putExtra(Constants.PREVIOUS_SCREEN, "manage");
									intent.putExtra(Constants.PACKAGE_NAME, packageName);
									intent.putExtra(Constants.KEY_REFERER, "direct");
									intent.putExtra(Constants.STORE_KEY, "apps");
									context.startActivity(intent);
									break;
								case 4:
									BackupApkTask task = new BackupApkTask(context);
									task.execute(packageName);
									break;
								case 5:
									Utils.installedAppDetails(context, packageName);
									break;
								}
							}
						});
						popupMenuCompat.show(v);
					}
				}
			}
		});
        return convertView;
    }

    private static class ItemViewHolder {
    	ImageView icon;
    	TextView name;
    	TextView currentVer;
    	TextView newVer;
    	ImageButton btnMenu;
    }
    
    @Override
	public boolean equals(Object o) {
    	if (o instanceof ListItem){
    		ListItem temp = (ListItem)o;
            if (this.packageName.equals(temp.getPackageName()))
                return true;
        }
        return false;
	}
    
	public interface IPassItem{
		public void onPassItem(ListItem item);
	}
	
	public interface UpdateClicked {
		public void onUpdateClicked(ListItem item);
	}
}
